/*
 *所有关于VOL_Demo_goodslx类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*VOL_Demo_goodslxService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Demo.IRepositories;
using System.Net;
using Microsoft.AspNetCore.Mvc.RazorPages;
using VOL.Core.ManageUser;

namespace VOL.Demo.Services
{
    public partial class VOL_Demo_goodslxService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IVOL_Demo_goodslxRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public VOL_Demo_goodslxService(
            IVOL_Demo_goodslxRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        private WebResponseContent webResponse = new WebResponseContent();



        public override PageGridData<VOL_Demo_goodslx> GetPageData(PageDataOptions options)
        {
            //2020.08.15
            //设置原生查询的sql语句，这里必须返回select * 表所有字段
            //（先内部过滤数据,内部调用EF方法FromSqlRaw,自己写的sql注意sql注入的问题），不会影响界面上提交的查询
            string date = DateTime.Now.AddYears(-10).ToString("yyyy-MM-dd");
            QuerySql = $@"select * from VOL_Demo_goodslx  
                                       where code='12'";
            return base.GetPageData(options);
        }
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            //AddOnExecute = (SaveModel saveModel) =>
            //{
            //    //如果返回false,后面代码不会再执行
            //    return webResponse.OK();
            //};
            // 在保存数据库前的操作，所有数据都验证通过了，这一步执行完就执行数据库保存
            AddOnExecuting = (VOL_Demo_goodslx goodslx, object list) =>
            {
                if (repository.Exists(x => x.goodsname == goodslx.goodsname))
                {
                    return webResponse.Error("分类名称已经存在");
                }
                //明细表对象
                //list<sellorderlist> orderlists = list as list<sellorderlist>;

                ////自定义逻辑
                //if (orderlists == null || orderlists.count == 0)
                //{//如果没有界面上没有填写明细，则中断执行
                //    return webresponse.error("必须填写明细数据");
                //}
                //if (orderlists.exists(x => x.qty <= 20))
                //    return webresponse.error("明细数量必须大于20");

                //设置webResponse.Code = "-1"会中止后面代码执行，与返回 webResponse.Error()一样，区别在于前端提示的是成功或失败
                //webResponse.Code = "-1";
                // webResponse.Message = "测试强制返回";
                //return webResponse.OK("ok");

                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
    }
}
